﻿/***************************************************COPYRIGHT***************************************
* Copyright (c) 2008, Whiteboard-IT
* All rights reserved.
*
* Redistribution and use in source and binary forms, with or without modification, are permitted provided that 
* the following conditions are met:
*
* Redistributions of source code must retain the above copyright notice, this list of conditions and the following 
 * disclaimer.
*
* Redistributions in binary form must reproduce the above copyright notice, this list of conditions and the following
 * disclaimer in the documentation and/or other materials provided with the distribution.
*
* Neither the name of Whiteboard-IT, LLC nor the names of its contributors may be used to endorse or promote 
 * products derived from this software without specific prior written permission.
*
* THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, 
 * INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE 
 * ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, 
 * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF 
 * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON 
 * ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) 
 * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. 
*****************************************************************************************************************/

using System;
using System.Collections.Generic;
using System.Linq;
using System.Reflection;
using Boo.Lang;
using Boo.Lang.Compiler.Ast;
using Boo.Lang.Compiler.Steps;

namespace Mite.Configuration
{
	public abstract class MiteSettings
	{
		protected MiteSettings()
		{
			AdapterFactory = new AdapterFactory();
			References = new List<Assembly>();
		}

		public MiteEnvironment[] Environments
		{
			get { return _environments.ToArray(); }
		}

		public IList<Assembly> References { get; private set; }
		public IAdapterFactory AdapterFactory { get; set; }

		#region Thorwaway properties

		protected Type Adapter { get; set; }
		protected string Database { get; set; }
		protected string Host { get; set; }
		protected string Username { get; set; }
		protected string Password { get; set; }
		protected Hash Options { get; set; }

		#endregion

		public abstract void Build();

		protected void Reference(string name)
		{
			References.Add(Assembly.LoadFile(name));
		}

		protected void Environment(string name, Action block)
		{
			block();
			var environment = new MiteEnvironment(name, Adapter, Host, Database, Username, Password, Options);
			_environments.Add(environment);
		}

		private readonly IList<MiteEnvironment> _environments = new List<MiteEnvironment>();
	}

	public class TransformVariablesInEnvironmentBlockToMemberReferenceExpressionsCompilerStep :
		AbstractTransformerCompilerStep
	{
		public override void Run()
		{
			Visit(CompileUnit);
		}

		private ReferenceTransformer referenceTransformer;

		#region Nested type: ReferenceTransformer

		private class ReferenceTransformer : DepthFirstTransformer
		{
			public ReferenceTransformer(ClassDefinition inst)
			{
				_inst = inst;
			}

			public override void OnReferenceExpression(ReferenceExpression node)
			{
				var classRef = new ReferenceExpression(node.LexicalInfo);
				classRef.Name = _inst.Name;

				var mre = new MemberReferenceExpression(node.LexicalInfo);
				mre.Name = node.Name;
				//				mre.Target = classRef;

				ReplaceCurrentNode(mre);
			}

			private readonly ClassDefinition _inst;
		}

		#endregion
	}
}